<?php

namespace app\api\model;


use think\Exception;
use think\Model;

class PatrolChild extends Model
{

    protected $name = 'patrol_child';

    /**
     * 打卡
     * @param $id
     * @param $data
     */
    public function savesSubmit($id, $patrol_id,$data = [])
    {
        try {

            $patrolModel = new Patrol();
            if ($this->db()->where('patrol_id' , $patrol_id)
                    ->where('status' , '2')
                    ->where('endstatus'  , '<>','1')
                    ->count('id')  < 1){

                $patrolModel->where('id' , $patrol_id)->update([
                    'actualstarttime' => $data['startime'],
                    'status' => 2,
                ]);
            }

            if ($this->db()->where('patrol_id' , $patrol_id)
                    ->where('status' , '1')
                    ->where('endstatus'  ,'1')
                    ->count('id')  == 1){
                $patrolModel->where('id' , $patrol_id)->update([
                    'actualendtime' => date('Y-m-d H:i:s'),
                    'status' => 3,
                ]);
            }

            $data['status'] = 2;
            $data['endtime'] = date('Y-m-d H:i:s');
            $result = $this->db()->removeOption()->where('id', $id)->update($data);
            if (!$result) {
                throw new Exception('打卡失败');
            }

        } catch (Exception $e) {
            throw $e;
        }
    }
}